﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace RuntimeDebugDraw.Internal
{
	// Token: 0x02000375 RID: 885
	internal class RuntimeDebugDraw : MonoBehaviour
	{
		// Token: 0x060016E6 RID: 5862 RVA: 0x00093580 File Offset: 0x00091780
		private void Awake()
		{
			this._ZTestBatch = new RuntimeDebugDraw.BatchedLineDraw(true);
			this._AlwaysBatch = new RuntimeDebugDraw.BatchedLineDraw(false);
			this._lineEntries = new List<RuntimeDebugDraw.DrawLineEntry>(16);
			this._textStyle = new GUIStyle();
			this._textStyle.alignment = TextAnchor.UpperLeft;
			this._drawTextEntries = new List<RuntimeDebugDraw.DrawTextEntry>(16);
			this._attachTextEntries = new List<RuntimeDebugDraw.AttachTextEntry>(16);
		}

		// Token: 0x060016E7 RID: 5863 RVA: 0x000935E3 File Offset: 0x000917E3
		private void OnGUI()
		{
			this.DrawTextOnGUI();
		}

		// Token: 0x060016E8 RID: 5864 RVA: 0x000935EB File Offset: 0x000917EB
		public void LateUpdate()
		{
			this.TickAndDrawLines();
			this.TickTexts();
		}

		// Token: 0x060016E9 RID: 5865 RVA: 0x000935F9 File Offset: 0x000917F9
		private void OnDestroy()
		{
			this._AlwaysBatch.Dispose();
			this._ZTestBatch.Dispose();
		}

		// Token: 0x060016EA RID: 5866 RVA: 0x00093614 File Offset: 0x00091814
		public void RegisterLine(Vector3 start, Vector3 end, Color color, float timer, bool noZTest)
		{
			RuntimeDebugDraw.DrawLineEntry drawLineEntry = null;
			for (int i = 0; i < this._lineEntries.Count; i++)
			{
				if (!this._lineEntries[i].occupied)
				{
					drawLineEntry = this._lineEntries[i];
					break;
				}
			}
			if (drawLineEntry == null)
			{
				drawLineEntry = new RuntimeDebugDraw.DrawLineEntry();
				this._lineEntries.Add(drawLineEntry);
			}
			drawLineEntry.occupied = true;
			drawLineEntry.start = start;
			drawLineEntry.end = end;
			drawLineEntry.color = color;
			drawLineEntry.timer = timer;
			drawLineEntry.noZTest = noZTest;
			this._linesNeedRebuild = true;
		}

		// Token: 0x060016EB RID: 5867 RVA: 0x000936B4 File Offset: 0x000918B4
		private void RebuildDrawLineBatchMesh()
		{
			this._ZTestBatch.Clear();
			this._AlwaysBatch.Clear();
			for (int i = 0; i < this._lineEntries.Count; i++)
			{
				RuntimeDebugDraw.DrawLineEntry drawLineEntry = this._lineEntries[i];
				if (drawLineEntry.occupied)
				{
					if (drawLineEntry.noZTest)
					{
						this._AlwaysBatch.AddLine(drawLineEntry.start, drawLineEntry.end, drawLineEntry.color);
					}
					else
					{
						this._ZTestBatch.AddLine(drawLineEntry.start, drawLineEntry.end, drawLineEntry.color);
					}
				}
			}
			this._ZTestBatch.BuildBatch();
			this._AlwaysBatch.BuildBatch();
		}

		// Token: 0x060016EC RID: 5868 RVA: 0x00093770 File Offset: 0x00091970
		private void TickAndDrawLines()
		{
			if (this._linesNeedRebuild)
			{
				this.RebuildDrawLineBatchMesh();
				this._linesNeedRebuild = false;
			}
			Graphics.DrawMesh(this._AlwaysBatch.mesh, Vector3.zero, Quaternion.identity, this._AlwaysBatch.mat, 4, null, 0, null, false, false);
			Graphics.DrawMesh(this._ZTestBatch.mesh, Vector3.zero, Quaternion.identity, this._ZTestBatch.mat, 4, null, 0, null, false, false);
			for (int i = 0; i < this._lineEntries.Count; i++)
			{
				RuntimeDebugDraw.DrawLineEntry drawLineEntry = this._lineEntries[i];
				if (drawLineEntry.occupied)
				{
					drawLineEntry.timer -= Time.deltaTime;
					if (drawLineEntry.timer < 0f)
					{
						drawLineEntry.occupied = false;
						this._linesNeedRebuild = true;
					}
				}
			}
		}

		// Token: 0x060016ED RID: 5869 RVA: 0x00093854 File Offset: 0x00091A54
		public void RegisterDrawText(Vector3 anchor, string text, Color color, int size, float timer, bool popUp)
		{
			RuntimeDebugDraw.DrawTextEntry drawTextEntry = null;
			for (int i = 0; i < this._drawTextEntries.Count; i++)
			{
				if (!this._drawTextEntries[i].occupied)
				{
					drawTextEntry = this._drawTextEntries[i];
					break;
				}
			}
			if (drawTextEntry == null)
			{
				drawTextEntry = new RuntimeDebugDraw.DrawTextEntry();
				this._drawTextEntries.Add(drawTextEntry);
			}
			drawTextEntry.occupied = true;
			drawTextEntry.anchor = anchor;
			drawTextEntry.content.text = text;
			drawTextEntry.size = size;
			drawTextEntry.color = color;
			RuntimeDebugDraw.DrawTextEntry drawTextEntry2 = drawTextEntry;
			drawTextEntry.timer = timer;
			drawTextEntry2.duration = timer;
			drawTextEntry.popUp = popUp;
			drawTextEntry.flag = RuntimeDebugDraw.DrawFlag.DrawnGizmo;
		}

		// Token: 0x060016EE RID: 5870 RVA: 0x00093908 File Offset: 0x00091B08
		public void RegisterAttachText(Transform target, Func<string> strFunc, Vector3 offset, Color color, int size)
		{
			RuntimeDebugDraw.AttachTextEntry attachTextEntry = null;
			for (int i = 0; i < this._attachTextEntries.Count; i++)
			{
				if (!this._attachTextEntries[i].occupied)
				{
					attachTextEntry = this._attachTextEntries[i];
					break;
				}
			}
			if (attachTextEntry == null)
			{
				attachTextEntry = new RuntimeDebugDraw.AttachTextEntry();
				this._attachTextEntries.Add(attachTextEntry);
			}
			attachTextEntry.occupied = true;
			attachTextEntry.offset = offset;
			attachTextEntry.transform = target;
			attachTextEntry.strFunc = strFunc;
			attachTextEntry.color = color;
			attachTextEntry.size = size;
			attachTextEntry.content.text = strFunc();
			attachTextEntry.flag = RuntimeDebugDraw.DrawFlag.DrawnGizmo;
		}

		// Token: 0x060016EF RID: 5871 RVA: 0x000939B8 File Offset: 0x00091BB8
		private void TickTexts()
		{
			for (int i = 0; i < this._drawTextEntries.Count; i++)
			{
				RuntimeDebugDraw.DrawTextEntry drawTextEntry = this._drawTextEntries[i];
				if (drawTextEntry.occupied)
				{
					drawTextEntry.timer -= Time.deltaTime;
					if (drawTextEntry.flag == RuntimeDebugDraw.DrawFlag.DrawnAll && drawTextEntry.timer < 0f)
					{
						drawTextEntry.occupied = false;
					}
				}
			}
			for (int j = 0; j < this._attachTextEntries.Count; j++)
			{
				RuntimeDebugDraw.AttachTextEntry attachTextEntry = this._attachTextEntries[j];
				if (attachTextEntry.occupied)
				{
					if (attachTextEntry.transform == null)
					{
						attachTextEntry.occupied = false;
						attachTextEntry.strFunc = null;
					}
					else if (attachTextEntry.flag == RuntimeDebugDraw.DrawFlag.DrawnAll)
					{
						attachTextEntry.content.text = attachTextEntry.strFunc();
						attachTextEntry.flag = RuntimeDebugDraw.DrawFlag.DrawnGizmo;
					}
				}
			}
		}

		// Token: 0x060016F0 RID: 5872 RVA: 0x00093ABC File Offset: 0x00091CBC
		private void DrawTextOnGUI()
		{
			Camera debugDrawCamera = Draw.GetDebugDrawCamera();
			if (debugDrawCamera == null)
			{
				return;
			}
			for (int i = 0; i < this._drawTextEntries.Count; i++)
			{
				RuntimeDebugDraw.DrawTextEntry drawTextEntry = this._drawTextEntries[i];
				if (drawTextEntry.occupied)
				{
					this.GUIDrawTextEntry(debugDrawCamera, drawTextEntry);
					RuntimeDebugDraw.DrawTextEntry drawTextEntry2 = drawTextEntry;
					drawTextEntry2.flag |= RuntimeDebugDraw.DrawFlag.DrawnGUI;
				}
			}
			for (int j = 0; j < this._attachTextEntries.Count; j++)
			{
				RuntimeDebugDraw.AttachTextEntry attachTextEntry = this._attachTextEntries[j];
				if (attachTextEntry.occupied)
				{
					this.GUIAttachTextEntry(debugDrawCamera, attachTextEntry);
					RuntimeDebugDraw.AttachTextEntry attachTextEntry2 = attachTextEntry;
					attachTextEntry2.flag |= RuntimeDebugDraw.DrawFlag.DrawnGUI;
				}
			}
		}

		// Token: 0x060016F1 RID: 5873 RVA: 0x00093B80 File Offset: 0x00091D80
		private void GUIDrawTextEntry(Camera camera, RuntimeDebugDraw.DrawTextEntry entry)
		{
			Vector3 anchor = entry.anchor;
			Vector3 v = camera.WorldToScreenPoint(anchor);
			v.y = (float)Screen.height - v.y;
			if (entry.popUp)
			{
				float num = entry.timer / entry.duration;
				v.y -= (1f - num * num) * (float)entry.size * 1.5f;
			}
			this._textStyle.normal.textColor = entry.color;
			this._textStyle.fontSize = entry.size;
			Rect position = new Rect(v, this._textStyle.CalcSize(entry.content));
			GUI.Label(position, entry.content, this._textStyle);
		}

		// Token: 0x060016F2 RID: 5874 RVA: 0x00093C48 File Offset: 0x00091E48
		private void GUIAttachTextEntry(Camera camera, RuntimeDebugDraw.AttachTextEntry entry)
		{
			if (entry.transform == null)
			{
				return;
			}
			Vector3 position = entry.transform.position + entry.offset;
			Vector3 v = camera.WorldToScreenPoint(position);
			v.y = (float)Screen.height - v.y;
			this._textStyle.normal.textColor = entry.color;
			this._textStyle.fontSize = entry.size;
			Rect position2 = new Rect(v, this._textStyle.CalcSize(entry.content));
			GUI.Label(position2, entry.content, this._textStyle);
		}

		// Token: 0x04001974 RID: 6516
		private List<RuntimeDebugDraw.DrawLineEntry> _lineEntries;

		// Token: 0x04001975 RID: 6517
		private RuntimeDebugDraw.BatchedLineDraw _ZTestBatch;

		// Token: 0x04001976 RID: 6518
		private RuntimeDebugDraw.BatchedLineDraw _AlwaysBatch;

		// Token: 0x04001977 RID: 6519
		private bool _linesNeedRebuild;

		// Token: 0x04001978 RID: 6520
		private List<RuntimeDebugDraw.DrawTextEntry> _drawTextEntries;

		// Token: 0x04001979 RID: 6521
		private List<RuntimeDebugDraw.AttachTextEntry> _attachTextEntries;

		// Token: 0x0400197A RID: 6522
		private GUIStyle _textStyle;

		// Token: 0x02000376 RID: 886
		private class DrawLineEntry
		{
			// Token: 0x0400197B RID: 6523
			public bool occupied;

			// Token: 0x0400197C RID: 6524
			public Vector3 start;

			// Token: 0x0400197D RID: 6525
			public Vector3 end;

			// Token: 0x0400197E RID: 6526
			public Color color;

			// Token: 0x0400197F RID: 6527
			public float timer;

			// Token: 0x04001980 RID: 6528
			public bool noZTest;
		}

		// Token: 0x02000377 RID: 887
		private class BatchedLineDraw : IDisposable
		{
			// Token: 0x060016F4 RID: 5876 RVA: 0x00093CF4 File Offset: 0x00091EF4
			public BatchedLineDraw(bool depthTest)
			{
				this.mesh = new Mesh();
				this.mesh.MarkDynamic();
				this.mat = new Material(Shader.Find("Hidden/Internal-Colored"));
				this.mat.SetInt("_ZTest", (!depthTest) ? 0 : 4);
				this._vertices = new List<Vector3>();
				this._colors = new List<Color>();
				this._indices = new List<int>();
			}

			// Token: 0x060016F5 RID: 5877 RVA: 0x00093D70 File Offset: 0x00091F70
			public void AddLine(Vector3 from, Vector3 to, Color color)
			{
				this._vertices.Add(from);
				this._vertices.Add(to);
				this._colors.Add(color);
				this._colors.Add(color);
				int count = this._vertices.Count;
				this._indices.Add(count - 2);
				this._indices.Add(count - 1);
			}

			// Token: 0x060016F6 RID: 5878 RVA: 0x00093DD5 File Offset: 0x00091FD5
			public void Clear()
			{
				this.mesh.Clear();
				this._vertices.Clear();
				this._colors.Clear();
				this._indices.Clear();
			}

			// Token: 0x060016F7 RID: 5879 RVA: 0x00093E03 File Offset: 0x00092003
			public void BuildBatch()
			{
				this.mesh.SetVertices(this._vertices);
				this.mesh.SetColors(this._colors);
				this.mesh.SetIndices(this._indices.ToArray(), MeshTopology.Lines, 0);
			}

			// Token: 0x060016F8 RID: 5880 RVA: 0x00093E3F File Offset: 0x0009203F
			public void Dispose()
			{
				UnityEngine.Object.Destroy(this.mesh);
				UnityEngine.Object.Destroy(this.mat);
			}

			// Token: 0x04001981 RID: 6529
			public Mesh mesh;

			// Token: 0x04001982 RID: 6530
			public Material mat;

			// Token: 0x04001983 RID: 6531
			private List<Vector3> _vertices;

			// Token: 0x04001984 RID: 6532
			private List<Color> _colors;

			// Token: 0x04001985 RID: 6533
			private List<int> _indices;
		}

		// Token: 0x02000378 RID: 888
		[Flags]
		public enum DrawFlag : byte
		{
			// Token: 0x04001987 RID: 6535
			None = 0,
			// Token: 0x04001988 RID: 6536
			DrawnGizmo = 1,
			// Token: 0x04001989 RID: 6537
			DrawnGUI = 2,
			// Token: 0x0400198A RID: 6538
			DrawnAll = 3
		}

		// Token: 0x02000379 RID: 889
		private class DrawTextEntry
		{
			// Token: 0x060016F9 RID: 5881 RVA: 0x00093E57 File Offset: 0x00092057
			public DrawTextEntry()
			{
				this.content = new GUIContent();
			}

			// Token: 0x0400198B RID: 6539
			public bool occupied;

			// Token: 0x0400198C RID: 6540
			public GUIContent content;

			// Token: 0x0400198D RID: 6541
			public Vector3 anchor;

			// Token: 0x0400198E RID: 6542
			public int size;

			// Token: 0x0400198F RID: 6543
			public Color color;

			// Token: 0x04001990 RID: 6544
			public float timer;

			// Token: 0x04001991 RID: 6545
			public bool popUp;

			// Token: 0x04001992 RID: 6546
			public float duration;

			// Token: 0x04001993 RID: 6547
			public RuntimeDebugDraw.DrawFlag flag;
		}

		// Token: 0x0200037A RID: 890
		private class AttachTextEntry
		{
			// Token: 0x060016FA RID: 5882 RVA: 0x00093E6A File Offset: 0x0009206A
			public AttachTextEntry()
			{
				this.content = new GUIContent();
			}

			// Token: 0x04001994 RID: 6548
			public bool occupied;

			// Token: 0x04001995 RID: 6549
			public GUIContent content;

			// Token: 0x04001996 RID: 6550
			public Vector3 offset;

			// Token: 0x04001997 RID: 6551
			public int size;

			// Token: 0x04001998 RID: 6552
			public Color color;

			// Token: 0x04001999 RID: 6553
			public Transform transform;

			// Token: 0x0400199A RID: 6554
			public Func<string> strFunc;

			// Token: 0x0400199B RID: 6555
			public RuntimeDebugDraw.DrawFlag flag;
		}
	}
}
